package com.ruoyi.api.controller;

import com.ruoyi.api.service.InterceptUtilService;
import com.ruoyi.api.service.InventoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.warehousing.domain.Warehousing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * PDA识别判断库存
 */
@Controller
@RequestMapping("/api/business/Inventory")
public class InventoryController extends BaseController {

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private InterceptUtilService interceptUtilService;

    /**
     * 截取扫描的型号唯一码
     */
    @PostMapping("/interceptStr")
    @ResponseBody
    public AjaxResult interceptStr(String[] barCode) {
        String intercept = interceptUtilService.intercept(barCode);
        if ("0".equals(intercept)) {
            return new AjaxResult(AjaxResult.Type.OTHER, "未截取到型号唯一码");
        } else {
            return new AjaxResult(AjaxResult.Type.SUCCESS, intercept);
        }

    }

    /**
     * @param barCode 唯一码
     * @return 返回分页信息
     */
    @PostMapping("/intercept")
    @ResponseBody
    public TableDataInfo intercept(String[] barCode) {
        startPage();
        String intercept = interceptUtilService.intercept(barCode);
        Warehousing warehousing = new Warehousing();
        warehousing.setModelCode(intercept);
//        List<Product> products = inventoryService.selectProductInventory(warehousing);
        List<Object> products = new ArrayList<>();
//        return getDataTable(products);
        return getDataTable2("", products);
//        startPage();
//        String intercept = interceptUtilService.intercept(barCode);
//        Warehousing warehousing = new Warehousing();
//        warehousing.setModelCode(intercept);
//        List<Product> products = inventoryService.selectProductInventory(warehousing);
//        return getDataTable2("", products);
    }

    /**
     * 查询产品库存信息
     */
    @PostMapping("/selectInventory")
    @ResponseBody
    public TableDataInfo list(Warehousing warehousing) {
        startPage();

        TableDataInfo tableDataInfo = inventoryService.selectProductInventory(warehousing);
        tableDataInfo.setCode(200);
//        List<Product> products = inventoryService.selectProductInventory(warehousing);
//        return getDataTable(products);
//        return getDataTable2("", products);
        return tableDataInfo;
    }

    /**
     * 重写分页数据
     *
     * @param msg  提示信息
     * @param list 数据
     * @return 结果
     */
    protected TableDataInfo getDataTable2(String msg, List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(list.size());
        return rspData;
    }

}
